﻿using System;
using Team4Mock.Entity;
using System.Data;

namespace Team4Mock.UserControl.Directorate
{
    public partial class ViewAll : System.Web.UI.UserControl
    {
        DirectorateEntity de = new DirectorateEntity();
        DepartmentEntity dd = new DepartmentEntity();
        OrganisationEntity oe = new OrganisationEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                dlNational.DataSource = oe.GetCountry();
                dlNational.DataBind();
                if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["Org"])))
                {

                }
                if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["s"])))
                {

                    int s = int.Parse(Request.QueryString["s"]);
                    dd.DirectorateID = s;
                    gvDepartment.DataSource = dd.GetDataById(dd);
                    gvDepartment.DataBind();

                    de.Directorateid = s;
                    DataTable dt = new DataTable();
                    dt = de.GetIDDirec(de);
                    txtDirecName.Text = dt.Rows[0][1].ToString();
                    txtDesShort.Text = dt.Rows[0][2].ToString();
                    idContact.Value = dt.Rows[0][3].ToString();
                    txtAdd1.Text = dt.Rows[0][4].ToString();
                    txtAdd2.Text = dt.Rows[0][5].ToString();
                    txtAdd3.Text = dt.Rows[0][6].ToString();
                    PostCode.Value = dt.Rows[0][7].ToString();
                    Town.Value = dt.Rows[0][8].ToString();
                    County.Value = dt.Rows[0][9].ToString();
                    dlNational.SelectedValue = dt.Rows[0][10].ToString();
                    hdidBus.Value = dt.Rows[0][11].ToString();
                    hdsic.Value = dt.Rows[0][12].ToString();
                    txtDesFull.Text = dt.Rows[0][13].ToString();
                    txtPhone.Text = dt.Rows[0][14].ToString();
                    txtFax.Text = dt.Rows[0][15].ToString();
                    txtWeb.Text = dt.Rows[0][16].ToString();
                    txtCharNum.Text = dt.Rows[0][17].ToString();
                    txtComNum.Text = dt.Rows[0][18].ToString();
                    if (dt.Rows[0][20].ToString() == "1")
                    {
                        isactive.Checked = true;
                    }
                    else
                    {
                        isactive.Checked = false;
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["Org"])))
            {
                string name = txtDirecName.Text, shortdes = txtDesShort.Text, bus = hdidBus.Value, sic = hdsic.Value, contactid = idContact.Value, full = txtDesFull.Text, orgid = Request.QueryString["Org"];
                string add1 = "", add2 = "", add3 = "", postcode = "", town = "", county = "", country = "", phone = "", fax = "", web = "", charity = "", company = "";
                if (ckCOpy.Checked == true)
                {
                    int idorg = int.Parse(Request.QueryString["Org"]);
                    de.OrganizationID = idorg;
                    DataTable dt = new DataTable();
                    dt = de.CopyOrganization(de);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        add1 = dt.Rows[0][0].ToString();
                        add2 = dt.Rows[0][1].ToString();
                        add3 = dt.Rows[0][2].ToString();
                        postcode = dt.Rows[0][3].ToString();
                        town = dt.Rows[0][4].ToString();
                        county = dt.Rows[0][5].ToString();
                        country = dt.Rows[0][6].ToString();
                        phone = dt.Rows[0][7].ToString();
                        fax = dt.Rows[0][8].ToString();
                        web = dt.Rows[0][9].ToString();
                        charity = dt.Rows[0][10].ToString();
                        company = dt.Rows[0][11].ToString();
                    }
                    de.DirectorateName = name;
                    de.DirectorateShortDescription = shortdes;
                    de.ContactID = int.Parse(contactid);
                    de.AddressLine1 = add1;
                    de.AddressLine2 = add2;
                    de.AddressLine3 = add3;
                    de.Postcode = postcode;
                    de.Town = town;
                    de.County = county;
                    de.Country = country;
                    de.TypeOfBusiness = bus;
                    de.SICCode = sic;
                    de.DirectorateFullDescription = full;
                    de.PhoneNumber = phone;
                    de.Fax = fax;
                    de.WebAddress = web;
                    de.CharityNumber = charity;
                    de.CompanyNumber = company;
                    de.OrganizationID = int.Parse(orgid);
                    if (isactive.Checked == true)
                    {
                        de.IsActive = 1;
                    }
                    else
                    {
                        de.IsActive = 0;
                    }
                    de.Insert(de);
                    tb.Text = "Insert success";
                }
                else
                {
                    postcode = PostCode.Value;
                    town = Town.Value;
                    county = County.Value;
                    country = dlNational.SelectedValue;

                    add1 = txtAdd1.Text;
                    add2 = txtAdd2.Text;
                    add3 = txtAdd3.Text;
                    phone = txtPhone.Text;
                    fax = txtFax.Text;
                    web = txtWeb.Text;
                    charity = txtCharNum.Text;
                    company = txtComNum.Text;
                    de.DirectorateName = name;
                    de.DirectorateShortDescription = shortdes;
                    de.ContactID = int.Parse(contactid);
                    de.AddressLine1 = add1;
                    de.AddressLine2 = add2;
                    de.AddressLine3 = add3;
                    de.Postcode = postcode;
                    de.Town = town;
                    de.County = county;
                    de.Country = country;
                    de.TypeOfBusiness = bus;
                    de.SICCode = sic;
                    de.DirectorateFullDescription = full;
                    de.PhoneNumber = phone;
                    de.Fax = fax;
                    de.WebAddress = web;
                    de.CharityNumber = charity;
                    de.CompanyNumber = company;
                    de.OrganizationID = int.Parse(orgid);
                    if (isactive.Checked == true)
                    {
                        de.IsActive = 1;
                    }
                    else
                    {
                        de.IsActive = 0;
                    }
                    de.Insert(de);
                    tb.Text = "Insert success";
                }
            }
            if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["s"])))
            {
                if (txtDirecName.Text == "" || txtAdd1.Text == "")
                {
                    tb.Text = "Input Directorate Name and Address Line 1";
                }
                else
                {
                    string name = txtDirecName.Text, shortdes = txtDesShort.Text, bus = hdidBus.Value, sic = hdsic.Value, contactid = idContact.Value, full = txtDesFull.Text, orgid = Request.QueryString["ID"];
                    string add1 = "", add2 = "", add3 = "", postcode = "", town = "", county = "", country = "", phone = "", fax = "", web = "", charity = "", company = "";
                    if (ckCOpy.Checked == true)
                    {
                        int idorg = int.Parse(Request.QueryString["ID"]);
                        de.OrganizationID = idorg;
                        DataTable dt = new DataTable();
                        dt = de.CopyOrganization(de);
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            add1 = dt.Rows[0][0].ToString();
                            add2 = dt.Rows[0][1].ToString();
                            add3 = dt.Rows[0][2].ToString();
                            postcode = dt.Rows[0][3].ToString();
                            town = dt.Rows[0][4].ToString();
                            county = dt.Rows[0][5].ToString();
                            country = dt.Rows[0][6].ToString();
                            phone = dt.Rows[0][7].ToString();
                            fax = dt.Rows[0][8].ToString();
                            web = dt.Rows[0][9].ToString();
                            charity = dt.Rows[0][10].ToString();
                            company = dt.Rows[0][11].ToString();
                        }
                        de.Directorateid = int.Parse(Request.QueryString["s"]);
                        de.DirectorateName = name;
                        de.DirectorateShortDescription = shortdes;
                        de.ContactID = int.Parse(contactid);
                        de.AddressLine1 = add1;
                        de.AddressLine2 = add2;
                        de.AddressLine3 = add3;
                        de.Postcode = postcode;
                        de.Town = town;
                        de.County = county;
                        de.Country = country;
                        de.TypeOfBusiness = bus;
                        de.SICCode = sic;
                        de.DirectorateFullDescription = full;
                        de.PhoneNumber = phone;
                        de.Fax = fax;
                        de.WebAddress = web;
                        de.CharityNumber = charity;
                        de.CompanyNumber = company;
                        de.OrganizationID = int.Parse(orgid);
                        if (isactive.Checked == true)
                        {
                            de.IsActive = 1;
                        }
                        else
                        {
                            de.IsActive = 0;
                        }
                        de.Update(de);
                        tb.Text = "Update success";
                    }
                    else
                    {
                        postcode = PostCode.Value;
                        town = Town.Value;
                        county = County.Value;
                        country = dlNational.SelectedValue;

                        add1 = txtAdd1.Text;
                        add2 = txtAdd2.Text;
                        add3 = txtAdd3.Text;
                        phone = txtPhone.Text;
                        fax = txtFax.Text;
                        web = txtWeb.Text;
                        charity = txtCharNum.Text;
                        company = txtComNum.Text;
                        de.Directorateid = int.Parse(Request.QueryString["s"]);
                        de.DirectorateName = name;
                        de.DirectorateShortDescription = shortdes;
                        de.ContactID = int.Parse(contactid);
                        de.AddressLine1 = add1;
                        de.AddressLine2 = add2;
                        de.AddressLine3 = add3;
                        de.Postcode = postcode;
                        de.Town = town;
                        de.County = county;
                        de.Country = country;
                        de.TypeOfBusiness = bus;
                        de.SICCode = sic;
                        de.DirectorateFullDescription = full;
                        de.PhoneNumber = phone;
                        de.Fax = fax;
                        de.WebAddress = web;
                        de.CharityNumber = charity;
                        de.CompanyNumber = company;
                        de.OrganizationID = int.Parse(orgid);
                        if (isactive.Checked == true)
                        {
                            de.IsActive = 1;
                        }
                        else
                        {
                            de.IsActive = 0;
                        }
                        de.Update(de);
                        tb.Text = "Update success";
                    }
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["Org"])))
            {
                Response.Redirect("/FormView/Organisation.aspx?ID=" + Request.QueryString["Org"]);
            }
            if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["ID"])))
            {
                Response.Redirect("/FormView/Organisation.aspx?ID=" + Request.QueryString["ID"]);
            }

        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Department.aspx?s=" + Request.QueryString["s"] + "&ID=" + Request.QueryString["ID"]);
        }
    }
}